package com.groupbuying.provider.controller;

import com.groupbuying.provider.context.FilterContextHandler;
import com.groupbuying.provider.dto.forManage.MenuDTO;
import com.groupbuying.provider.dto.forManage.StoreMenuDO;
import com.groupbuying.provider.dto.forManage.Tree;
import com.groupbuying.provider.service.StoreMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ywxd
 * @version V1.0
 */
@RequestMapping(value = "/menu", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
public class MenuController {
    @Autowired
    private StoreMenuService storeMenuService;


    //@Log("访问菜单")
    @GetMapping("tree")
    Tree<StoreMenuDO> tree(){
        return storeMenuService.getTree();
    }

    @GetMapping
    List<Tree<StoreMenuDO>> list(){
        return storeMenuService.getTree().getChildren();
    }

    @GetMapping("{id}")
    StoreMenuDO get(@PathVariable("id") String id) {
        StoreMenuDO menu = storeMenuService.get(id);
        return menu;
    }

    @GetMapping("list")
    List<StoreMenuDO> list(@RequestParam Map<String, Object> params) {
        List<StoreMenuDO> menus = storeMenuService.list(params);
        return menus;
    }

    @GetMapping("userMenus")
    List<MenuDTO> userMenus(){
        List<StoreMenuDO> menuDOS = storeMenuService.userMenus(FilterContextHandler.getUserID());
        List<MenuDTO> menuDTOS = new ArrayList<MenuDTO>();

        for (StoreMenuDO menuDO:menuDOS){
            MenuDTO menuDTO = new MenuDTO();
            menuDTO.setMenuId(menuDO.getMenuId());
            menuDTO.setUrl(menuDO.getUrl());
            menuDTO.setPerms(menuDO.getPerms());
            menuDTOS.add(menuDTO);
        }

        return menuDTOS;
    }

    /**
     * 当前用户菜单的树形结构
     * @return
     */
    @RequestMapping("/currentUserMenus")
    List<Tree<StoreMenuDO>> currentUserMenus() {
        List<Tree<StoreMenuDO>> menus = storeMenuService.listMenuTree(FilterContextHandler.getUserID());
        return menus;
    }

    /**
     * 获取角色下的权限所有id
     * @return
     */
    @GetMapping("/roleId")
    List<BigInteger> menuIdsByRoleId(String roleId){
        return storeMenuService.MenuIdsByRoleId(roleId);
    }


}
